package com.alex.learnspringbootjdbc.jdbc.service;

import com.alex.learnspringbootjdbc.jdbc.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<User> listUser() {
        String sql = "select * from user";

        List<User> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class));



        // List<User> list = jdbcTemplate.query(sql, new RowMapper<User>() {
        //     @Override
        //     public User mapRow(ResultSet resultSet, int i) throws SQLException {
        //         User user = new User();
        //         user.setId(resultSet.getInt("id"));
        //         user.setUsername(resultSet.getString("username"));
        //         user.setPassword(resultSet.getString("password"));
        //         user.setBir(resultSet.getDate("bir"));
        //         return user;
        //     }
        // });

        return list;
    }
}
